package com.bookocean.infrastructure.persistence.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.bookocean.infrastructure.persistence.po.NovelChapterPO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDateTime;

/**
 * 小说章节 Mapper 接口
 * 
 * @author BookOcean
 * @since 2024-01-01
 */
@Mapper
public interface NovelChapterMapper extends BaseMapper<NovelChapterPO> {

    /**
     * 获取小说的下一个章节序号
     * 
     * @param novelId 小说ID
     * @return 下一个章节序号
     */
    @Select("SELECT COALESCE(MAX(chapter_number), 0) + 1 FROM novel_chapter_po WHERE novel_id = #{novelId}")
    Integer getNextChapterNumber(@Param("novelId") Long novelId);

    /**
     * 检查用户今日是否已写过章节
     * 
     * @param novelId 小说ID
     * @param authorId 作者ID
     * @param todayStart 今日开始时间
     * @return 章节数量
     */
    @Select("SELECT COUNT(*) FROM novel_chapter_po WHERE novel_id = #{novelId} AND author_id = #{authorId} " +
            "AND created_at >= #{todayStart}")
    Integer countTodayChapters(@Param("novelId") Long novelId, 
                              @Param("authorId") Long authorId, 
                              @Param("todayStart") LocalDateTime todayStart);
}
